package com.hao.study.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hao.study.entity.Thing;
import com.hao.study.entity.ThingTag;
import com.hao.study.mapper.ThingMapper;
import com.hao.study.mapper.ThingTagMapper;
import com.hao.study.service.ThingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ThingServiceImpl extends ServiceImpl<ThingMapper, Thing> implements ThingService {
    @Autowired
    ThingMapper thingMapper;
    @Autowired
    ThingTagMapper thingTagMapper;
    @Override
    public PageInfo<Thing> getThingList(Integer pageNum,Integer pageSize,String keyword, String sort, String classification, String tag) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Thing> queryWrapper =new QueryWrapper<>();

        //搜索
        queryWrapper.like(StringUtils.isNotBlank(keyword),"title",keyword);
        System.out.println(keyword);
        //排序
        if (StringUtils.isNotBlank(sort)){
            if (sort.equals("recent")){
                queryWrapper.orderBy(true,false,"create_time");
            }else if (sort.equals("hot")||sort.equals("recommend")){
                queryWrapper.orderBy(true,false,"pv");
            }
        }else {
            queryWrapper.orderBy(true,false,"create_time");
        }

        //根据分类筛选
        if (StringUtils.isNotBlank(classification)&&!classification.equals("-1")){
            queryWrapper.eq(true,"classification_id",classification);
        }

        List<Thing> thingList = thingMapper.selectList(queryWrapper);

        //根据标签筛选
        if (StringUtils.isNotBlank(tag)){
            List<Thing> things=new ArrayList<>();
            QueryWrapper<ThingTag> thingTagQueryWrapper=new QueryWrapper<>();
            thingTagQueryWrapper.eq("tag_id",tag);
            List<ThingTag> thingTagList=thingTagMapper.selectList(thingTagQueryWrapper);
            for (Thing thing :thingList) {
                for (ThingTag thingTag : thingTagList) {
                    if (thing.getId().equals(thingTag.getThingId())){
                        things.add(thing);
                    }
                }
            }
            thingList.clear();
            thingList.addAll(things);
        }

        //附加tag
        for (Thing thing : thingList){
            QueryWrapper<ThingTag> thingTagQueryWrapper=new QueryWrapper<>();
            thingTagQueryWrapper.lambda().eq(ThingTag::getThingId,thing.getId());
            List<ThingTag> thingTags=thingTagMapper.selectList(thingTagQueryWrapper);
            List<Long> tags=thingTags.stream().map(ThingTag::getTagId).collect(Collectors.toList());
            thing.setTags(tags);
        }
        System.out.println("thingList   ==============>    "+thingList);

        return PageInfo.of(thingList);
    }

    @Override
    public Thing getThingById(String id) {
        return thingMapper.selectById(id);
    }

    @Override
    public void createThing(Thing thing) {
        System.out.println(thing);
        thing.setCreateTime(String.valueOf(System.currentTimeMillis()));
        if (thing.getPv() == null) {
            thing.setPv("0");
        }

        if (thing.getWishCount() == null) {
            thing.setWishCount("0");
        }
        thingMapper.insert(thing);
        // 更新tag
        setThingTags(thing);
    }

    @Override
    public void deleteThing(String id) {
        thingMapper.deleteById(id);
    }

    @Override
    public void updateThing(Thing thing) {
        // 更新tag
        setThingTags(thing);

        thingMapper.updateById(thing);
    }

    @Override
    public List<Thing> getUserThing(String userId) {
        QueryWrapper<Thing> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return thingMapper.selectList(queryWrapper);
    }

    private void setThingTags(Thing thing) {
        Map<String,Object> map=new HashMap<>();
        map.put("thing_id",thing.getId());
        thingTagMapper.deleteByMap(map);
        if (thing.getTags()!=null){
            for (Long tag : thing.getTags()){
                ThingTag thingTag=new ThingTag();
                thingTag.setThingId(thing.getId());
                thingTag.setTagId(tag);
                thingTagMapper.insert(thingTag);
            }
        }
    }
}
